package com.zh.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.net.InterfaceAddress;
import java.util.Properties;

/**
 * 创建连接用的对象,整合mybatis到spring-->
 * <bean class="org.mybatis.spring.SqlSessionFactoryBean">
 * <property name="dataSource" ref="dataSource"/>
 * <property name="typeAliasesPackage" value="com.zh.domain"/>
 * 映射配置扫描-->
 * <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 * <property name="basePackage" value="com.zh.dao"/>
 * </bean>
 */
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setDataSource(dataSource);
        ssfb.setTypeAliasesPackage("com.zh.domain");
        /**
         *  分页插件-->
         *  <property name="plugins">
         *  <array>
         *  <bean class="com.github.pagehelper.PageInterceptor">
         *    <property name="properties">
         *   <props>
         *   <prop key="helperDialect">mysql</prop>
         *   <prop key="reasonable">true</prop>
         *   </props>
         *   </property>
         *   </bean>
         *  </array>
         *  </property>
         */
        Interceptor i = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect","mysql");
        properties.setProperty("reasonable", "true");
        i.setProperties(properties);
        ssfb.setPlugins(i);
        return ssfb;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.zh.dao");
        return msc;
    }
}
